﻿
CREATE PROCEDURE [dbo].[resuma_InchideLuna] 
	-- Add the parameters for the stored procedure here
	@Luna smallint, 
	@An smallint,
	@CodUnitate smallint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	--SET NOCOUNT ON;
	-- numarul mijloacelor fixe cu acelasi numar de inventar

	DECLARE @azi DateTime
	SET @azi = CAST(CAST(@An as varchar(4)) + '-' + cast(@Luna as varchar(2)) + '-01' AS DateTime)
 
	BEGIN TRAN
	

	UPDATE [ResUmaSetup] SET [LunaCurenta]= MONTH(DATEADD(month,1,@azi)),[AnulCurent]=YEAR(DATEADD(month,1,@azi))
	WHERE [CodUnitate]=@CodUnitate
	IF @@ERROR <>0 GOTO Eroare 
	
	DECLARE @RC int
	DECLARE @AnulN smallint
	DECLARE @LunaN smallint

	SELECT @AnulN=[AnulCurent],@LunaN = [LunaCurenta] FROM [ResUmaSetup] WHERE [CodUnitate]=@CodUnitate
	IF @@ERROR <>0 GOTO Eroare 

	EXECUTE @RC = [resuma_InitializareTotalPontaj] 
		@CodUnitate,@AnulN,@LunaN
	IF @@ERROR <>0 GOTO Eroare 

	COMMIT

	---inchidere luna

	GOTO Gata
	DUBLURI:
		RAISERROR ('Exista mijloace fixe cu acelasi numar de inventar',16,-1)
		GOTO Gata

	EROARE:
		ROLLBACK
		RAISERROR ('Eroare la prelucrarea datelor',16,-1)
		GOTO Gata

		GATA:
END